package com.epam.task.web.util.logging;

import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.Logger;
import java.io.FileNotFoundException;
import java.io.IOException;

public class LoggerUtil {

    private Logger exceptionTextFileLogger;
    private FileAppender textFileAppender;

    public LoggerUtil(Logger logger) {
        this.exceptionTextFileLogger = logger;

        this.exceptionTextFileLogger.setLevel(Level.DEBUG);

        try {
            if ((this.textFileAppender = new FileAppender(new SimpleLayout(),
                    System.getProperty("user.dir") + String.format("\\log\\%s", "log.txt"))) == null) {
                throw new FileNotFoundException();
            }
        } catch (FileNotFoundException exception) {
            System.out.println("There are not found file /log/log.txt");
        } catch (IOException exception) {
            System.out.println("Unknown exception in LoggerUtil");
        }

        this.exceptionTextFileLogger.addAppender(this.textFileAppender);
    }

    public Logger getExceptionTextFileLogger() {
        return exceptionTextFileLogger;
    }

}
